//
//  PLXQViewController.m
//  OneWordNew
//
//  Created by qianfeng001 on 15-7-9.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "PLXQViewController.h"
#import "AFNetworking.h"
#import "LoginViewController.h"
@interface PLXQViewController ()
{
    AFHTTPRequestOperationManager *_manager;

}

@property (weak, nonatomic) IBOutlet UITextView *textViewSend;
@end

@implementation PLXQViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
}
- (void)creatUI{
    UIBarButtonItem *send=[[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(btnClick:)];
    
    UIBarButtonItem *clear=[[UIBarButtonItem alloc] initWithTitle:@"重输" style:UIBarButtonItemStyleDone target:self action:@selector(clearClick:)];
    
    self.navigationItem.rightBarButtonItems=@[send,clear];
    _manager=[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
}
- (void)clearClick:(UIBarButtonItem *)btn{
self.textViewSend.text=@"";

}
- (void)btnClick:(UIBarButtonItem *)btn{
    [self senPL];
}
- (void)senPL{
    
    NSString *usToken=[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    
    __weak typeof(self)weakSelf=self;
    
    NSString *mytoken=[usToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"%@",usToken);
    NSLog(@"%@",mytoken);
    if (usToken==nil) {
        LoginViewController *login=[[LoginViewController alloc] init];
        
        [self.navigationController pushViewController:login animated:YES];
        return;
    }
    if (self.textViewSend.text.length==0) {
        [self showAliterWithTitle:@"提示" message:@"请输入内容"];
        return;
    }
    NSLog(@"%@",mytoken);
    NSLog(@"%@",self.textViewSend.text);
    NSLog(@"%@",self.userId);
    NSLog(@"%@",mytoken);
    NSDictionary *dict=@{@"token":mytoken,
                         @"content":self.textViewSend.text,
                         @"key":@"3a4b2a12539a916c040d069ae8ac8310",
                         @"ceshi_id":self.userId
                         };
    NSLog(@"%@",dict);
    NSString *url=[kSedPinLURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"%@",url);
    [_manager POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                if ([dict[@"code"] isEqual:@(0)]) {
                [weakSelf showAliterWithTitle:@"恭喜" message:@"发表成功"];
                weakSelf.textViewSend.text=@" ";
                    [self.navigationController popViewControllerAnimated:YES];
                
            }
        }
        else{
            
            [self showAliterWithTitle:@"提示" message:@"发表失败"];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"shibai");
        [self showAliterWithTitle:@"提示" message:@"检查网络"];
        
    }];
    
    
}
- (void)showAliterWithTitle:(NSString *)title message:(NSString *)message{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
